package com.define.ssh.servlet;

import java.io.IOException;
import java.util.Arrays;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.define.ssh.util.SHA1;

/**
 * 微信信息接收口
 * 
 * @author wuwencong
 * 
 */
public class WeiXinServlet extends HttpServlet {

	// public WeiXinService weixinService;

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public WeiXinServlet() {
		super();
	}

	@Override
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		System.out.println("doget");
		System.out.println("----------get---------------");
		final String signature = req.getParameter("signature");
		final String timestamp = req.getParameter("timestamp");
		final String nonce = req.getParameter("nonce");
		final String echostr = req.getParameter("echostr");
		System.out.println("signature:" + signature);
		System.out.println("timestamp:" + timestamp);
		System.out.println("nonce:" + nonce);
		System.out.println("echostr:" + echostr);

		// 对数据进行验证
		final String token = "dfzy";
		final String[] array = { token, timestamp, nonce };
		Arrays.sort(array);// 按照字母进行排序
		String result = ""; // 将排序后的三个字符串，拼成一个字符串
		for (final String i : array) {
			result += i;
		}
		new SHA1();
		final String ret = SHA1.byte2hex(result.getBytes());

		if (signature.equals(ret)) {
			System.out.println("验证通过");
		} else {
			System.out.println("验证失败");
		}
		System.out.println(ret);
		resp.getWriter().write(echostr);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		System.out.println("dopost");
	}

	@Override
	public void init() throws ServletException {
		// Put your code here
	}

}